package com.geekteam.masterenglish.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import com.geekteam.masterenglish.provider.EEContract.AlbumColumns;
import com.geekteam.masterenglish.provider.EEContract.LessionColumns;
import com.geekteam.masterenglish.provider.EEContract.SyncColumns;

/**
 * @author MTROL
 *
 */
public class EEDatabase extends SQLiteOpenHelper{

	private static final String DATABASE_NAME = "mEnglish.db";

	private static final int VER_LAUNCH = 4;
	private static final int VER_APP_TYPE = 5;

	private static final int DATABASE_VERSION = VER_APP_TYPE;
	
	public interface Tables{
		String LESSIONS = "lessions";
		String ALBUMS = "albums";
	}
	
	public EEDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE "+ Tables.LESSIONS+"("+ BaseColumns._ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ LessionColumns.ID + " TEXT NOT NULL, "
				+ LessionColumns.TITLE + " TEXT, "
				+ LessionColumns.DESCRIPTION + " TEXT, "
				+ LessionColumns.TEXT + " TEXT, "
				+ LessionColumns.LINK_128 + " TEXT, "
				+ LessionColumns.LINK_320 + " TEXT, "
				+ LessionColumns.LINKDOWN_128 + " TEXT, "
				+ LessionColumns.LINKDOWN_320 + " TEXT, "
				+ LessionColumns.COVER + " TEXT, "
				+ LessionColumns.TIME_LENGTH + " INTEGER, "
				+ LessionColumns.ISFAVORITE + " INTEGER, "
				+ LessionColumns.TOTAL_LISTEN + " INTEGER, "
				+ LessionColumns.ALBUM_ID + " TEXT, "
				+ LessionColumns.ALBUM_NAME + " TEXT, "
				+ SyncColumns.UPDATED + " LONG,"
				+ "UNIQUE (" + LessionColumns.ID + ") ON CONFLICT REPLACE)");
		
		db.execSQL("CREATE TABLE "+ Tables.ALBUMS+"("+ BaseColumns._ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ AlbumColumns.ID + " TEXT NOT NULL, "
				+ AlbumColumns.COVER + " TEXT, "
				+ AlbumColumns.TITLE + " TEXT, "
				+ AlbumColumns.BOOK_ID + " TEXT, "
				+ AlbumColumns.TOTAL_LISTEN + " INTEGER, "
				+ "UNIQUE (" + AlbumColumns.ID + ") ON CONFLICT REPLACE)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		int version = oldVersion;

		switch (version) {
		case VER_LAUNCH:
		}

		if (version != DATABASE_VERSION) {
			db.execSQL("DROP TABLE IF EXISTS " + Tables.LESSIONS);
			db.execSQL("DROP TABLE IF EXISTS " + Tables.ALBUMS);
		}
		onCreate(db);
	}

	public static void deleteDatabase(Context context) {
		context.deleteDatabase(DATABASE_NAME);
	}
}
